package com.wyvoid.forum.web.action.controlPanel;

import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.service.IFriendService;
import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.annotations.LoginUser;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.domain.Friend;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: wangchao
 * Date: 2007-7-1
 * Time: 15:49:14
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding("/controlPanel/friend.do")
@LoginUser
public class FriendAction extends BaseAction {
    @SpringBean
    private IFriendService friendService;
     @SpringBean
    private IUserService userService;
    private User user;
    private long friendId;
    //private Friend friend;

    public void setFriendService(IFriendService friendService) {
        this.friendService = friendService;
    }


    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public long getFriendId() {
        return friendId;
    }

    public void setFriendId(long friendId) {
        this.friendId = friendId;
    }
     @DefaultHandler
     public Resolution addFriend() {
        Friend friend;
        friend=friendService.getFriendByFriendUserId(friendId);
        if(friend==null){
        friend=new Friend();
        User friendUser = userService.getUser(friendId);
        friend.setCreateDate(new Date());
        friend.setFriendUser(friendUser);
        friend.setUid(getContext().getUserId());
        friendService.createFriend(friend);
        return new RedirectResolution("/user/member.do?userData&userId="+friendId);
        }else{
            return new RedirectResolution("/user/member.do?userData&userId="+friendId);
        }
    }
}
